package cn.neusoft.service.impl;

import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.mapper.BaseMapper;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.service.IBaseService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Service
public class BaseService<T> implements IBaseService<T> {

	@Autowired
	BaseMapper<T> baseMapper;

	@Override
	public ResultBean save(T t)  {
		int count;
		//这里利用反射进行录
		Class<?> tclass=t.getClass();
		String name=tclass.getSimpleName();
		String methodName="get"+name+"Id";
        Method method= null;
        try {
            method = tclass.getMethod(methodName);
			Object id = method.invoke(t);
			if(id==null) {
				count=baseMapper.add(t);
			} else {
				count=baseMapper.update(t);
			}
        } catch (Exception e) {
			e.printStackTrace();
           throw new AppException();
        }
		if(count==0) {
			throw new AppException(AppExceptionCodeMsg.SAVE_FAIL);
		}
		return ResultBean.SUCCESS();
	}

	@Override
	public ResultBean add(T t) throws Exception {
		if(t==null) {
			throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
		}
		int count=baseMapper.add(t);
		if(count==0) {
			throw new AppException(AppExceptionCodeMsg.INSERT_FAIL);
		}
		return ResultBean.SUCCESS();
	}

	@Override
	public ResultBean update(T t) throws Exception {
		if(t==null) {
			throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
		}
		int count=baseMapper.update(t);
		if(count==0) {
			throw new AppException(AppExceptionCodeMsg.UPDATE_FAIL);
		}
		return ResultBean.SUCCESS();
	}

	@Override
	public ResultBean delete(Integer id) throws Exception {
		if(id==null) {
			throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
		}
		int count=baseMapper.delete(id);
		if(count==0) {
			throw new AppException(AppExceptionCodeMsg.DELETE_FAIL);
		}
		return ResultBean.SUCCESS();
	}

	@Override
	public ResultBean getById(Integer id) throws Exception {
		if(id == null) {
			throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
		}
		T t = baseMapper.getById(id);
		if(t==null) {
			throw new AppException(AppExceptionCodeMsg.DATA_NOT_FOUND);
		}
		return t!=null? ResultBean.SUCCESS(t) : ResultBean.ERROR("查询失败");
	}

	@Override
	public ResultBean getAll(BaseQuery baseQuery) throws Exception {
		if(baseQuery==null) {
			throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
		}
		Page<T> page= PageHelper.startPage(baseQuery.getPageNow(),baseQuery.getPageCount());
		List<T> list=baseMapper.getAll(baseQuery);
		PageBean<T> pageBean=new PageBean<>();

		pageBean.setData(list);
		pageBean.setTotalRows(page.getTotal());


		return ResultBean.SUCCESS(pageBean);
	}

	@Override
	public ResultBean batchDel(List<Integer> ids) throws Exception {
		if(ids==null) {
			throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
		}
		for (Integer id :ids) {
			int delete = baseMapper.delete(id);
			if(delete==0) {
				throw new AppException(AppExceptionCodeMsg.DELETE_FAIL);
			}
		}
		return ResultBean.SUCCESS();
	}
}
